﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Document_Management
{
    public partial class frmSongjianjilu : Form
    {
        static string connectionString = ConfigurationManager.ConnectionStrings["庫存管理"].ConnectionString;
        SqlConnection con = new SqlConnection(connectionString);
        string tbs0, tbs1, tbs2, tbs3, tbs4, tbs5, tbs6, tbs7, tbs8, tbs9;
        string tbn1, tbn2, tbn3, tbn4, tbn5, tbn6, tbn7, tbn8, tbn9;
        string Songjian;
        string str = null;
        bool b量具Search, b量具Insert, b量具Edit, b量具Del;
        public frmSongjianjilu(bool b量具Search, bool b量具Insert, bool b量具Edit, bool b量具Del)
        {
            InitializeComponent();
            this.b量具Search = b量具Search;
            this.b量具Insert = b量具Insert;
            this.b量具Edit = b量具Edit;
            this.b量具Del = b量具Del;

        }

        private void frmSongjianjilu_Load(object sender, EventArgs e)
        {
            if (!b量具Insert)
            {
                btnTra.Enabled = false;
                btnAddnew3.Enabled = false;
                btn_add.Enabled = false;
            }
            if (!b量具Search)
            {
                btnSearch送檢.Enabled = false;
            }
            if (!b量具Edit)
            {
                btn_edit.Enabled = false;
                btnEdit3.Enabled = false;
            }
            if (!b量具Del)
            {
                btn_del.Enabled = false;
                btnDelete3.Enabled = false;
            }
            load檢定機構();
            ResetText檢定機構();
            load辦理人員();
            ResetText辦理人員();
            load送檢紀錄();
            ResetText送檢紀錄();
        }
        public void load檢定機構(){
            SqlConnection con = new SqlConnection(connectionString);
            try
            {
                con.Open();
                SqlDataAdapter da = new SqlDataAdapter("sp_檢定機構_load", con);
                da.SelectCommand.CommandType = CommandType.StoredProcedure;
                DataTable dt = new DataTable();
                da.Fill(dt);
                Dav檢定機構.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
                Dav檢定機構.DataSource = dt;
                //Dav檢定機構.Columns[0].Width = 0;
                PublicClass.setRowNumber(Dav檢定機構);
                con.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                con.Close();
            }
        }

        public void ResetText檢定機構(){
            txt檢定機構代號.ResetText();
            txt檢定機構名稱.ResetText();
        }

        public void load辦理人員()
        {
            SqlConnection con = new SqlConnection(connectionString);
            try
            {
                con.Open();
                SqlDataAdapter da = new SqlDataAdapter("sp_辦理人員_load", con);
                da.SelectCommand.CommandType = CommandType.StoredProcedure;
                DataTable dt = new DataTable();
                da.Fill(dt);
                dataGV辦理人.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
                dataGV辦理人.DataSource = dt;
                //Dav檢定機構.Columns[0].Width = 0;
                PublicClass.setRowNumber(dataGV辦理人);
                con.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                con.Close();
            }
        }

        public void ResetText辦理人員()
        {
            txt辦理人代號.ResetText();
            //txt檢定機構代號2.ResetText();
            txt辦理人姓名.ResetText();
        }

        public void load送檢紀錄()
        {
            SqlConnection con = new SqlConnection(connectionString);
            try
            {
                con.Open();
                SqlDataAdapter da = new SqlDataAdapter("sp_送檢_load", con);
                da.SelectCommand.CommandType = CommandType.StoredProcedure;
                DataTable dt = new DataTable();
                da.Fill(dt);
                //dataGV送檢紀錄.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
                dataGV送檢紀錄.DataSource = dt;
                dataGV送檢紀錄.Columns[0].Width = 0;
                PublicClass.setRowNumber(dataGV送檢紀錄);
                con.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                con.Close();
            }
        }

        public void ResetText送檢紀錄()
        {
            dateTimePicker_From_送檢日期.Value = dateTimePicker_From_送檢日期.MinDate;
            todate.Value = todate.MaxDate;
            dateTimePicker_from_檢定日期.Value = dateTimePicker_from_檢定日期.MinDate;
            todate2.Value = todate2.MaxDate;
            txt量具編號.ResetText();
            txt檢定機構.ResetText();
            txt量具名稱.ResetText();
            txt辦理人員.ResetText();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnSearch領用_Click(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection(connectionString);
            try
            {
                //if (Ck領用日期.Checked == true)
                //{

                //    //領用紀錄load();
                //}
                //if (Ck歸還日期.Checked == true)
                //{

                //    //領用紀錄load();
                //}
                con.Open();
                string sp = "sp_送檢_尋找";
                SqlCommand cmd = new SqlCommand(sp, con);
                cmd.CommandType = CommandType.StoredProcedure;
                if (txt量具編號.Text != "") cmd.Parameters.AddWithValue("@量具編號", txt量具編號.Text);
                if (txt量具名稱.Text.ToString() != "") cmd.Parameters.AddWithValue("@量具名稱", txt量具名稱.Text);
                //if (dateTimePicker_From_送檢日期.Text != "") cmd.Parameters.AddWithValue("@送檢日期", DateTime.Parse(dateTimePicker_From_送檢日期.Text));
                //if (dateTimePicker_from_檢定日期.Text != "") cmd.Parameters.AddWithValue("@檢定日期", DateTime.Parse(dateTimePicker_from_檢定日期.Text));
                if (txt辦理人員.Text != "") cmd.Parameters.AddWithValue("@人員代號", txt辦理人員.Text);
                if (txt檢定機構.Text.ToString() != "") cmd.Parameters.AddWithValue("@檢定機構代號", txt檢定機構.Text);
                cmd.Parameters.AddWithValue("@datefrom", dateTimePicker_From_送檢日期.Value);
                cmd.Parameters.AddWithValue("@todate", todate.Value);
                cmd.Parameters.AddWithValue("@datefrom2", dateTimePicker_from_檢定日期.Value);
                cmd.Parameters.AddWithValue("@todate2", todate2.Value);

                SqlDataReader sqlreader = cmd.ExecuteReader();
                DataTable dt = new DataTable();
                //dt.Columns.Add("項次");//add new 項次 column
                dt.Columns.Add("項次");
                dt.Columns.Add("量具編號");
                dt.Columns.Add("量具狀態");
                dt.Columns.Add("量具名稱");
                dt.Columns.Add("規格型號");
                dt.Columns.Add("數量");
                dt.Columns.Add("廠牌代號");
                dt.Columns.Add("測量精度");
                dt.Columns.Add("測量範圍");
                dt.Columns.Add("出廠日期");
                dt.Columns.Add("校正類別代號");
                dt.Columns.Add("送檢日期");
                dt.Columns.Add("檢定日期");
                dt.Columns.Add("檢定機構代號");
                dt.Columns.Add("檢定結果");
                dt.Columns.Add("有效日期");
                dt.Columns.Add("檢定費用");
                dt.Columns.Add("人員代號");
                dt.Columns.Add("備註");
                //int k=1;
                while (sqlreader.Read())
                {
                    DataRow dtrows = dt.NewRow();
                    dtrows["項次"] = sqlreader["項次"];
                    dtrows["量具狀態"] = sqlreader["量具狀態"];
                    dtrows["量具狀態"] = sqlreader["量具狀態"];
                    dtrows["量具名稱"] = sqlreader["量具名稱"];
                    dtrows["規格型號"] = sqlreader["規格型號"];
                    dtrows["數量"] = sqlreader["數量"];
                    dtrows["廠牌代號"] = sqlreader["廠牌代號"];
                    dtrows["測量精度"] = sqlreader["測量精度"];
                    dtrows["測量範圍"] = sqlreader["測量範圍"];
                    dtrows["出廠日期"] = sqlreader["出廠日期"];
                    dtrows["校正類別代號"] = sqlreader["校正類別代號"];
                    dtrows["送檢日期"] = sqlreader["送檢日期"];
                    dtrows["檢定日期"] = sqlreader["檢定日期"];
                    dtrows["檢定機構代號"] = sqlreader["檢定機構代號"];
                    dtrows["檢定結果"] = sqlreader["檢定結果"];
                    dtrows["有效日期"] = sqlreader["有效日期"];
                    dtrows["檢定費用"] = sqlreader["檢定費用"];
                    dtrows["人員代號"] = sqlreader["人員代號"];
                    dtrows["備註"] = sqlreader["備註"];         
                    dt.Rows.Add(dtrows);
                    //k++;
                }
                dataGV送檢紀錄.DataSource = dt;
                PublicClass.setRowNumber(dataGV送檢紀錄);
                dataGV送檢紀錄.Columns[0].Width = 0;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                con.Close();
            }
        }

        private void btnReset_Click(object sender, EventArgs e)
        {
            ResetText送檢紀錄();
            load送檢紀錄();
        }

        private void btnTra_Click(object sender, EventArgs e)
        {
            try{
            Songjian = "2";
            int r = dataGV送檢紀錄.CurrentCell.RowIndex;
            string id0 = dataGV送檢紀錄.Rows[r].Cells[0].Value.ToString();
            string id1 = dataGV送檢紀錄.Rows[r].Cells[1].Value.ToString();
            string id6 = dataGV送檢紀錄.Rows[r].Cells[2].Value.ToString();
            string id2 = dataGV送檢紀錄.Rows[r].Cells[3].Value.ToString();
            string id3 = dataGV送檢紀錄.Rows[r].Cells[4].Value.ToString();
            string id4 = dataGV送檢紀錄.Rows[r].Cells[5].Value.ToString();
            string id5 = dataGV送檢紀錄.Rows[r].Cells[6].Value.ToString();
            string id7 = dataGV送檢紀錄.Rows[r].Cells[7].Value.ToString();
            string id8 = dataGV送檢紀錄.Rows[r].Cells[8].Value.ToString();
            string id9 = dataGV送檢紀錄.Rows[r].Cells[9].Value.ToString();
            string id10 = dataGV送檢紀錄.Rows[r].Cells[10].Value.ToString();
            string id11 = dataGV送檢紀錄.Rows[r].Cells[11].Value.ToString();
            string id12 = dataGV送檢紀錄.Rows[r].Cells[12].Value.ToString();
            string id13 = dataGV送檢紀錄.Rows[r].Cells[13].Value.ToString();
            string id14 = dataGV送檢紀錄.Rows[r].Cells[14].Value.ToString();
            string id15 = dataGV送檢紀錄.Rows[r].Cells[15].Value.ToString();
            string id16 = dataGV送檢紀錄.Rows[r].Cells[16].Value.ToString();
            string id17 = dataGV送檢紀錄.Rows[r].Cells[17].Value.ToString();
            string id18 = dataGV送檢紀錄.Rows[r].Cells[18].Value.ToString();
            tbs0 = id0;
            tbs1 = id1; //tb1 0
            tbs2 = id2; //cb1 1
            tbs3 = id3; //tb2 2
            tbs4 = id4; //tb3 3
            tbs5 = id5; //cb2 4
            tbs6 = id6;
            tbs7 = id7; //tb3 3
            tbs8 = id8; //cb2 4
            tbs9 = id9;
            tbn1 = id10;
            tbn2 = id11; //tb1 0
            tbn3 = id12; //cb1 1
            tbn4 = id13; //tb2 2
            tbn5 = id14; //tb3 3
            tbn6 = id15; //cb2 4
            tbn7 = id16;
            tbn8 = id17; //tb3 3
            tbn9 = id18; //tb3 3

            frmSongjian frm = new frmSongjian(Songjian, tbs0, tbs1, tbs2, tbs3, tbs4, tbs5, tbs6, tbs7, tbs8, tbs9, tbn1, tbn2, tbn3, tbn4, tbn5, tbn6, tbn7, tbn8, tbn9, b量具Search, b量具Insert, b量具Edit, b量具Del);
            frm.ShowDialog();
            load送檢紀錄();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void txt量具編號_TextChanged(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection(connectionString);
            try
            {
                con.Open();
                String temp = "";
                string strcmd = String.Format("select * from 量具訊息 where 量具編號=N'{0}'", txt量具編號.Text);
                SqlCommand cmd = new SqlCommand(strcmd, con);
                var x = cmd.ExecuteReader();
                //int icount=0;
                while (x.Read()) temp = x[2].ToString();
                if (temp == "")
                {
                    LbName量具.Text = "";
                    txt量具名稱.Text = "";
                }
                else LbName量具.Text = temp; txt量具名稱.Text = temp;
                con.Close();
            }
            catch (Exception ex) { MessageBox.Show(ex.Message); }
        }

        private void btnSearch量具_Click(object sender, EventArgs e)
        {
            frmSearchLiangjubianhao frm = new frmSearchLiangjubianhao(txt量具編號);
            frm.ShowDialog();
        }

        private void txt受理人_TextChanged(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection(connectionString);
            try
            {
                con.Open();
                String temp = "";
                string strcmd = String.Format("select * from 辦理人員 where 人員代號=N'{0}'", txt辦理人員.Text);
                SqlCommand cmd = new SqlCommand(strcmd, con);
                var x = cmd.ExecuteReader();
                //int icount=0;
                while (x.Read()) temp = x[1].ToString();
                if (temp == "") lbName辦理人員.Text = "";
                else lbName辦理人員.Text = temp;
                con.Close();
            }
            catch (Exception ex) { MessageBox.Show(ex.Message); }
        }

        private void btnSearch受理人_Click(object sender, EventArgs e)
        {
            frmSearchBanliRenYuan frm = new frmSearchBanliRenYuan(txt辦理人員);
            frm.ShowDialog();
        }

        private void dataGV送檢紀錄_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            PublicClass.setRowNumber(dataGV送檢紀錄);
        }

        private void tab送檢紀錄_SelectedIndexChanged(object sender, EventArgs e)
        {
            PublicClass.setRowNumber(Dav檢定機構);
            ResetText檢定機構();
            PublicClass.setRowNumber(dataGV辦理人);
            ResetText辦理人員();
            PublicClass.setRowNumber(dataGV送檢紀錄);
            ResetText送檢紀錄();
        }

        /// <summary>
        /// 檢定機構
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="PhucTG"></param>
        private void btn_add_Click(object sender, EventArgs e)
        {
            try
            {
                if (txt檢定機構代號.Text == "")
                {
                    MessageBox.Show("檢定機構代號不正確", "錯誤", MessageBoxButtons.OK);
                    txt檢定機構代號.Focus();
                    return;
                }
                //bat dau insert vao database

                con.Open();
                string sp_insert = "sp_檢定機構_新增";
                SqlCommand cmd = new SqlCommand(sp_insert, con);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@檢定機構代號", txt檢定機構代號.Text);
                cmd.Parameters.AddWithValue("@姓名", txt檢定機構名稱.Text);
                cmd.ExecuteNonQuery();
                MessageBox.Show("新增成功");
                load檢定機構();
                ResetText檢定機構();
                con.Close();
                //reset all text
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                load檢定機構();
                return;
            }
        }

        private void btn_edit_Click(object sender, EventArgs e)
        {
            try
            {
                if (txt檢定機構代號.Text == "") { MessageBox.Show("請您輸入檢定機構代號更新"); }
                else
                {
                    con.Open();
                    SqlCommand cmd2;
                    String sql = "SELECT COUNT(*) FROM 檢定機構 WHERE 檢定機構代號=N'" + str + "'";
                    cmd2 = new SqlCommand(sql, con);
                    if ((int)cmd2.ExecuteScalar() > 0)
                    {
                        string sp_update = "sp_檢定機構_修改";
                        cmd2 = new SqlCommand(sp_update, con);
                        cmd2.CommandType = CommandType.StoredProcedure;
                        //con.Open();
                        cmd2.Parameters.AddWithValue("@檢定機構代號", str);
                        cmd2.Parameters.AddWithValue("@檢定機構代號2", txt檢定機構代號.Text);
                        cmd2.Parameters.AddWithValue("@姓名", txt檢定機構名稱.Text);
                        cmd2.ExecuteNonQuery();
                        MessageBox.Show("更新成功");
                        load檢定機構();
                        ResetText檢定機構();
                        con.Close();
                        return;
                    }
                    else
                    {
                        MessageBox.Show("Error! '" + txt檢定機構代號.Text.ToString() + "' 不由來");
                        con.Close();
                        return;
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                load檢定機構();
                return;
            }
        }

        private void btn_del_Click(object sender, EventArgs e)
        {
            try
            {
                if (txt檢定機構代號.Text == "") { MessageBox.Show("請您輸入檢定機構代號刪除"); }
                else
                {

                    DialogResult answer = MessageBox.Show("您確定要出入檢定機構代號碼", "警報", MessageBoxButtons.YesNo);
                    if (answer == DialogResult.Yes)
                    {
                        con.Open();
                        string sp_delete = "sp_檢定機構_刪除";
                        SqlCommand sqldelete = new SqlCommand(sp_delete, con);
                        sqldelete.CommandType = CommandType.StoredProcedure;
                        sqldelete.Parameters.AddWithValue("@檢定機構代號", txt檢定機構代號.Text);
                        MessageBox.Show("刪除成功");
                        sqldelete.ExecuteNonQuery();
                        load檢定機構();
                        ResetText檢定機構();
                        con.Close();
                        return;
                    }
                    else
                    {
                        load檢定機構();
                        con.Close();
                        return;
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
                load檢定機構();
                con.Close();
                return;
            }
        }

        private void btnReset4_Click(object sender, EventArgs e)
        {
            load檢定機構();
            ResetText檢定機構();
        }

        private void Dav檢定機構_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            PublicClass.setRowNumber(Dav檢定機構);
        }

        private void Dav檢定機構_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                int r = e.RowIndex;
                str = Dav檢定機構.Rows[r].Cells[0].Value.ToString();
                txt檢定機構代號.Text = Dav檢定機構.Rows[r].Cells[0].Value.ToString();
                txt檢定機構名稱.Text = Dav檢定機構.Rows[r].Cells[1].Value.ToString();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnAddnew3_Click(object sender, EventArgs e)
        {
            try
            {
                if (txt辦理人代號.Text == "")
                {
                    MessageBox.Show("檢定辦理人員代號不正確", "錯誤", MessageBoxButtons.OK);
                    txt辦理人代號.Focus();
                    return;
                }
                //bat dau insert vao database

                con.Open();
                string sp_insert = "sp_辦理人員_新增";
                SqlCommand cmd = new SqlCommand(sp_insert, con);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@人員代號", txt辦理人代號.Text);
                cmd.Parameters.AddWithValue("@姓名", txt辦理人姓名.Text);
                cmd.ExecuteNonQuery();
                MessageBox.Show("新增成功");
                load辦理人員();
                ResetText辦理人員();
                con.Close();
                //reset all text
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                load辦理人員();
                return;
            }
        }

        private void btnEdit3_Click(object sender, EventArgs e)
        {
            try
            {
                if (txt辦理人代號.Text == "") { MessageBox.Show("請您輸入辦理人員代號更新"); }
                else
                {
                    con.Open();
                    SqlCommand cmd2;
                    String sql = "SELECT COUNT(*) FROM 辦理人員 WHERE 人員代號=N'" + str + "'";
                    cmd2 = new SqlCommand(sql, con);
                    if ((int)cmd2.ExecuteScalar() > 0)
                    {
                        string sp_update = "sp_辦理人員_修改";
                        cmd2 = new SqlCommand(sp_update, con);
                        cmd2.CommandType = CommandType.StoredProcedure;
                        //con.Open();
                        cmd2.Parameters.AddWithValue("@人員代號", str);
                        cmd2.Parameters.AddWithValue("@人員代號2", txt辦理人代號.Text);
                        cmd2.Parameters.AddWithValue("@姓名", txt辦理人姓名.Text);
                        cmd2.ExecuteNonQuery();
                        MessageBox.Show("更新成功");
                        load辦理人員();
                        ResetText辦理人員();
                        con.Close();
                        return;
                    }
                    else
                    {
                        MessageBox.Show("Error! '" + txt辦理人代號.Text.ToString() + "' 不由來");
                        con.Close();
                        return;
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                load辦理人員();
                return;
            }
        }

        private void btnDelete3_Click(object sender, EventArgs e)
        {
            try
            {
                if (txt辦理人代號.Text == "") { MessageBox.Show("請您輸入辦理人員代號刪除"); }
                else
                {

                    DialogResult answer = MessageBox.Show("您確定要出入辦理人員代號碼", "警報", MessageBoxButtons.YesNo);
                    if (answer == DialogResult.Yes)
                    {
                        con.Open();
                        string sp_delete = "sp_辦理人員_刪除";
                        SqlCommand sqldelete = new SqlCommand(sp_delete, con);
                        sqldelete.CommandType = CommandType.StoredProcedure;
                        sqldelete.Parameters.AddWithValue("@人員代號", txt辦理人代號.Text);
                        MessageBox.Show("刪除成功");
                        sqldelete.ExecuteNonQuery();
                        load辦理人員();
                        ResetText辦理人員();
                        con.Close();
                        return;
                    }
                    else
                    {
                        load辦理人員();
                        con.Close();
                        return;
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
                load辦理人員();
                con.Close();
                return;
            }
        }

        private void btnReset3_Click(object sender, EventArgs e)
        {
            ResetText辦理人員();
            load辦理人員();
            txt辦理人代號.ResetText();
            txt辦理人姓名.ResetText();
        }

        private void dataGV辦理人_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            PublicClass.setRowNumber(dataGV辦理人);
        }

        private void dataGV辦理人_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                int r = e.RowIndex;
                str = dataGV辦理人.Rows[r].Cells[0].Value.ToString();
                txt辦理人代號.Text = dataGV辦理人.Rows[r].Cells[0].Value.ToString();
                txt辦理人姓名.Text = dataGV辦理人.Rows[r].Cells[1].Value.ToString();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return;
            }
        }

        private void tbnSearch檢定機構_Click(object sender, EventArgs e)
        {
            frmSearchJiandingJigou frm = new frmSearchJiandingJigou(txt檢定機構);
            frm.ShowDialog();
        }

        private void txt檢定機構_TextChanged(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection(connectionString);
            try
            {
                con.Open();
                String temp = "";
                string strcmd = String.Format("select * from 檢定機構 where 檢定機構代號=N'{0}'", txt檢定機構.Text);
                SqlCommand cmd = new SqlCommand(strcmd, con);
                var x = cmd.ExecuteReader();
                //int icount=0;
                while (x.Read()) temp = x[1].ToString();
                if (temp == "") lbName檢定機構.Text = "";
                else lbName檢定機構.Text = temp;
                con.Close();
            }
            catch (Exception ex) { MessageBox.Show(ex.Message); }
        }
     
    }
}
